﻿using Abp.AspNetCore.Mvc.Controllers;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.UI;
using BRMC.IntelligentCommunity.App;
using BRMC.IntelligentCommunity.Authorization.Users;
using BRMC.IntelligentCommunity.FaceRecognitions;
using BRMC.IntelligentCommunity.FaceRecognitions.Dto;
using BRMC.IntelligentCommunity.IntelligentCommunity.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BRMC.IntelligentCommunity.Web.Controllers
{
  public  class FaceRecognitionController : IntelligentCommunityAppServiceBase
    {
        private readonly IRepository< FaceRecognition,long> _faceRecognition;

        private readonly IRepository<IdentifiedimfInfo ,long>_identifiedimfInfo;

        private readonly IRepository<User_base_info,long> _user_base_info;
        public FaceRecognitionController(IRepository<FaceRecognition, long> faceRecognition, IRepository<IdentifiedimfInfo, long> identifiedimfInfo, IRepository<User_base_info, long> user_base_info )
        {
            _faceRecognition = faceRecognition;
            _identifiedimfInfo = identifiedimfInfo;
            _user_base_info = user_base_info;
        }
        [HttpPost]
        [Route("register")]
        public async Task Register(List<FaceRecognitionDto> faceRecognitionInfos)
        {
            if (faceRecognitionInfos.Count != 0)
            {
                foreach (var item in faceRecognitionInfos)
                {

                    var user = await _user_base_info.FirstOrDefaultAsync(c => c.Identification.ToMd5() == item.Idcardper);
                 
                    var faceRecognition = new FaceRecognitionDto();

              var      faceRecognitionInfo = ObjectMapper.Map<FaceRecognition>(item);
                    faceRecognitionInfo.UserId = user.Id;
           await            _faceRecognition.InsertAsync(faceRecognitionInfo);
                }
            }  else{
                throw new UserFriendlyException(L("没有上传内容"));
            }
        }

        [HttpPost]
        [Route("editFaceRecognition")]
        public async Task EditFaceRecognition(List<FaceRecognitionDto> faceRecognitionInfos)
        {
            if (faceRecognitionInfos.Count != 0)
            {
                foreach (var item in faceRecognitionInfos)
                {



                    var faceRecognition = await _faceRecognition.FirstOrDefaultAsync(c => c.Idcardper == item.Idcardper);

                    faceRecognition = ObjectMapper.Map(item, faceRecognition);

                    await _faceRecognition.UpdateAsync(faceRecognition);
                }
            }
            else
            {
                throw new UserFriendlyException(L("没有上传内容"));
            }
        }


        [HttpPost]
        [Route("deleteFaceRecognition")]
        public async Task DeleteFaceRecognition(FaceRecognitionDto faceRecognitionInfos)
        {
            if (faceRecognitionInfos!=null)
            {
              


                    var faceRecognition = await _faceRecognition.FirstOrDefaultAsync(c => c.Idcardper == faceRecognitionInfos.Idcardper);

                if (faceRecognition != null)
                {
                    await _faceRecognition.DeleteAsync(faceRecognition.Id);

                }
                else
                {
                    throw new UserFriendlyException(L("无此信息"));
                }


            }
            else
            {
                throw new UserFriendlyException(L("删除失败"));
            }
        }

        [HttpPost]
        [Route("Identifiedimf")]

        public async Task Identifiedimf(IdentifiedimfDto IdentifiedimfInfo)
        {

            if (IdentifiedimfInfo != null)
            {
                var Identifiedimf = new IdentifiedimfInfo();
                Identifiedimf= ObjectMapper.Map(IdentifiedimfInfo, Identifiedimf);
           //     var Identifiedimf = ObjectMapper.Map<IdentifiedimfInfo>(IdentifiedimfInfo);

                await _identifiedimfInfo.InsertAsync(Identifiedimf);
            }
            else
            {
                throw new UserFriendlyException(L("回传信息不能为空"));
            }
          
        }
    }
}
